/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.odhixon.volarme.facades;

import com.odhixon.volarme.persistence.Persistence;
import com.odhixon.volarme.persistence.RegistroPerst;
import com.odhixon.volarme.persistence.Root;
import org.garret.perst.Key;

/**
 * RegistrosFachada.
 * @author Ariel Aguirre <odhixon@gmail.com>
 */
public class RegistrosFachada {

    static public RegistrosFachada instance = null;

    static public RegistrosFachada getInstance() {
        if (instance == null) {
            instance = new RegistrosFachada();
        }
        return instance;
    }

    public Object[] registrosList() {
        Persistence persistence = Persistence.getInstance();
        return persistence.getRoot().getIdxRegistros().toPersistentArray();
    }

    public void save(RegistroPerst registro) {
        Persistence persistence = Persistence.getInstance();
        Root root = persistence.getRoot();
        if (registro.getId() == -1) {
            registro.setId(root.getProximoRegistroId());
            root.setProximoRegistroId(root.getProximoRegistroId() + 1);
            root.getIdxRegistros().put(new Key(registro.getId()), registro);
            root.modify();
        } else {
            registro.modify();
        }
        persistence.commit();
    }
}
